<template>
	<view class="mainPage	whitePage" :style="{'padding-top': `${$store.state.safeArea.top || 0}px`, 'padding-bottom': `${$store.state.safeArea.bottom || 0}px`}">
		<public-head :title="title"></public-head>
		<view class="DrugsTop">
			<view class="input-box">
				<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/search.png" mode=""></image>
				<input focus v-model.trim="searchValue"  type="text" value="" placeholder="请输入药品名称" placeholder-style="font-size: 24rpx;color: #AAA1A1;" />
				<text class="search" @click="search">搜索</text>
			</view>
		</view>
		<view class="DrugsMain" v-if="show">
			<scroll-view class="DrugsMainRight" scroll-y="true">
				<view class="drugslist" v-if="yaoList.length">
					<view v-for="(item,index) in yaoList" :key="index" class="list">
						<view class="titlelist" @click="xiangqing(item.dr_id)">
							<view class="imgs" >
								<view class="types"  v-if="item.added==1">已加</view>
								<image :src="item.dr_img">
								</image>
							</view>
							<view class="title">
								<view class="name">{{item.dr_name}}</view>
								<view class="specifications">{{item.dr_specifications}}</view>
								<view class="stock">库存：{{item.dr_stock}}</view>
								<view class="pricelist">
									<view class="price">¥{{item.dr_price}}</view>
									<view class="Rebate">返利 ¥{{item.dr_rebate}}</view>
									<view style="position: absolute;
									right: 40rpx;">
										<image v-if="item.dr_stock!=0"
											src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/img/btn_add.png"
											style="width: 52rpx;height: 52rpx;" @click.stop="addmedicine(item.drugs_id,item.dr_specifications)">
										</image>
										<image v-if="item.dr_stock==0"
											src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/img/btn_adds.png"
											style="width: 52rpx;height: 52rpx;" @click.stop="tishi"></image>
									</view>
								</view>
								<view style="
								color: #909090;
								font-size: 24rpx;text-decoration:line-through;">¥{{item.dr_original_price}}</view>
							</view>
						</view>
						<view class="anniu" v-if="item.is_Collection==0" @click.stop="jiaru(item.dr_id,index)">设为常用药
						</view>
					</view>
				</view>
				<view class="zanwuyuyue" v-else>
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/kong.png" mode=""></image>
					<text>暂无该药品信息</text>
				</view>
			</scroll-view>
		</view>
		<u-popup v-model="showadd" mode="bottom" border-radius="25" height="1216rpx" :closeable="true">
			<view style="background-color: #FFFFFF; height:1216rpx; padding: 0rpx 32rpx;">
				<view class="u-skeleton">
					<view class="box">
						<view class="box1">
							<view class="img" v-if="yaoDetail.dr_img && yaoDetail.dr_img.length">
								<image :src="yaoDetail.dr_img[0]" mode=""></image>
							</view>
							<view class="info">
								<view class="name">
									{{yaoDetail.dr_name}}
									{{yaoDetail.dr_model}}
								</view>
								<view class="gongsi">
									{{yaoDetail.dr_brand}}
								</view>
								<view class="price">
									¥{{yaoDetail.dr_price}}
								</view>
							</view>
						</view>
						<view class="box2">
							<view class="shuliang-box">
								<view class="left">
									<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode=""></image>
									<text>购买数量</text>
								</view>
								<view class="right">
									<image @click="jianNum" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jian.png" mode=""></image>
									<text>{{number}}</text>
									<image @click="jiaNum(yaoDetail.dr_stock)" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jia.png" mode=""></image>
								</view>
							</view>
							<view class="yongfa-box">
								<view class="left">
									<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode=""></image>
									<text>药品用法</text>
								</view>
								<view class="right">
									<input type="number" v-model.trim="explain" placeholder="请输入数量" placeholder-style="color: #C7C3C3;font-size: 24rpx;" />
									<view class="wenben">
										<!-- <text v-if="riNum==1">日</text> -->
										<!-- <text v-if="riNum==2">周</text> -->
										<!-- <text v-if="riNum==3">月</text> -->
										<text>次/</text>
										<text class="shijian" :class="riNum==1?'active':''"  @click.stop="riTab(1)">每日</text>
										<text class="shijian" :class="riNum==2?'active':''"  @click.stop="riTab(2)" >每周</text>
										<text class="shijian" :class="riNum==3?'active':''"  @click.stop="riTab(3)">每月</text>
									</view>
									<!-- <image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jiantou.png" mode=""></image> -->
								</view>
							</view>
							<view class="yongliang-box">
								<view class="left">
									<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode=""></image>
									<text>单次用量</text>
								</view>
								<view class="right">
									<input type="digit" v-model.trim="Num2" placeholder="请输入数量" placeholder-style="color: #C7C3C3;font-size: 24rpx;" />
									<view class="wenben" >
										<!-- <text v-if="pianNum==1">片</text> -->
										<!-- <text v-if="pianNum==2">包</text> -->
										<!-- <text v-if="pianNum==3">瓶</text> -->
										<text class="shijian" :class="pianNum==1?'active':''"  @click.stop="pianTab(1)">片</text>
										<text class="shijian" :class="pianNum==2?'active':''"  @click.stop="pianTab(2)" >包</text>
										<text class="shijian" :class="pianNum==3?'active':''"  @click.stop="pianTab(3)">瓶</text>
										<text>/次</text>	
									</view>
									<!-- <image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jiantou.png" mode=""></image> -->
								</view>
							</view>
							<view class="banfa-box">
								<view class="left">
									<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode=""></image>
									<text>使用办法</text>
								</view>
								<view class="right">
									<view class="wenben">
										<!-- <text v-if="neiNum==1">内服</text> -->
										<!-- <text v-if="neiNum==2">外用</text> -->
										<!-- <text v-if="neiNum==3">注射</text> -->
										<text class="shijian" :class="neiNum==1?'active':''"  @click.stop="neiTab(1)">内服</text>
										<text class="shijian" :class="neiNum==2?'active':''"  @click.stop="neiTab(2)" >外用</text>
										<text class="shijian" :class="neiNum==3?'active':''"  @click.stop="neiTab(3)">注射</text>
									</view>
									<!-- <image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jiantou.png" mode=""></image> -->
								</view>
							</view>
							<view class="beizhu-box">
								<image style="width: 20rpx; height: 20rpx;display: inline-block;" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode=""></image>
								<view class="title">
									用法用量
								</view>
								<textarea v-model.trim="explain" placeholder="请输入用法用量"  placeholder-style="font-size: 32rpx;" />
							</view>
							<view class="beizhu-box">
								<view class="title" style="margin-left: 20rpx;">
									备注
								</view>
								<textarea v-model.trim="remarks" placeholder="备注说明" placeholder-style="font-size: 32rpx;" />
							</view>
						</view>
					</view>
					<view class="btn-box">
						<view class="btn" @click="shiyong">
							使用
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	var that
	import { mapState } from 'vuex'
	import publicHead from "@/components/publicHead/publicHead.vue"
	export default {
		components: {
			publicHead
		},
		data() {
			return {
				title: '搜索药品',
				searchValue:"",
				yaoList:[],
				show:false,
				yaoId:'',
				showadd: false,
				
				tabNum: 0,
				changyongyaoList: [],
				quanbuList: [],
				drugtype: "",
				isactive: -2, //是否选中 -2为默认
				isactives: -1,
				showcard:false,
				yaoDetail: '',
				goumaiNum: 1,
				 aaa:0,
				show1: false,
				riNum: 1,
				show2: false,
				pianNum: 1,
				show3: false,
				neiNum: 1,
				Num1: "",
				Num2: "",
				beizhu: "",
				yaoList:'',
				
				leixing: 1, ////传进来的类型  /1是添加 2修改
				
				cengNum: 2, ///返回跳转的层数
				number: 1,
				explain:'',
				remarks:'',
				added:0
				
				
				
			
				
			}
		},
		onLoad() {
			that=this
			// that.yaoList=that.$store.state.yaoList
		},
		methods: {
			search(){
				that.yaoList=[]
				if(that.searchValue==""){
					uni.showToast({
						icon:"none",
						title:"搜索内容不能为空！"
					})
					return
				}
				that.$postAjax1('Doctorsside_lookup_drugs',{
					drugs_name:that.searchValue,
				},function(data){  //https://ask.suoweilai.com/Doctorsside_lookup_drugs 医生端-药品 - 医生端-医生查询药品
					// console.log(data.data.data,99999999999)
					that.yaoList=data.data.data
					
					console.log('开始循环')
					for (var j = 0; j < that.yaoList.length; j++){
						console.log('进入循环')
					for (var i = 0; i <that.$store.state.yaoList.length; i++) {
						
						
						if (that.$store.state.yaoList[i].drugs_id == that.yaoList[j].drugs_id) {
							
							that.yaoList[j].added=1
							console.log('drugs_id',that.$store.state.yaoList[i].drugs_id,that.yaoList[j].drugs_id)
						}
						}
						
					}
					
					
					console.log(11111111111)
					that.show=true
				})
			},
			
			xiangqing(drugs_id) { //药品详情
				uni.navigateTo({
					url: "./drugDetail?drugs_id=" + drugs_id
				})
			},
			jiaru(yaoId){  //加入常用药
				console.log("加入常用药")
				
				that.$postAjax1('Doctorsside_collectcancel_drugs',{
					drugs_id:yaoId,
					status:0
				},function(data){  //https://ask.suoweilai.com/Doctorsside_collectcancel_drugs  医生端-药品 - 医生端-收藏或删除药品
					uni.showToast({
					    title: '添加成功',
					    duration: 2000,
						icon:"none"
					});
					that.search()
				})
			},
			addmedicine(id) {
				that.showadd = true
				that.yaoId = id
				that.drugs_id = id
				that.editdrugs(id)
			},
			editdrugs() {
				that.$postAjax1('Doctorsside_drugs_info', {
					drugs_id: that.yaoId,
				}, function(data) { //https://ask.suoweilai.com/Doctorsside_drugs_info  医生端-药品 - 医生端-医生药品详情
					that.yaoDetail = data.data.data
			
					// console.log(data.data.data,99999999999)
				})
			},
			jianNum() {
				if (that.number == 1) {
					return
				}
				that.number--
			},
			
			
			jiaNum(dr_stock) {
				if(that.number>=dr_stock){
					
					uni.showToast({
						title: '数量超出范围~~',
						icon:'none',
						duration: 2000
					});
					
					return
				}
				that.number++
				
			},
			
			meiri() {
				that.show1 = true
			},
			ci() {
				that.show2 = true
			},
			xuanze() {
				that.show3 = true
			},
			riTab(num) {
				that.riNum = num
				
				console.log('riNum日周月',that.riNum)
				that.show1 = false
			},
			pianTab(num) {
				that.pianNum = num
				if(num==1){
					console.log('片',that.pianNum)
				}
				if(num==2){
					console.log('包',that.pianNum)
				}
				if(num==3){
					console.log('瓶',that.pianNum)
				}
				that.show2 = false
			},
			neiTab(num) {
				that.neiNum = num
				that.show3 = false
			},
			
			shiyong() {
							
							
							if (that.explain == "") {
								uni.showToast({
									icon: "none",
									title: "用法用量不能为空!"
								})
								return
							}
							// if (that.Num2 == "") {
							// 	uni.showToast({
							// 		icon: "none",
							// 		title: "单次用量不能为空!"
							// 	})
							// 	return
							// }
			
							var datas = [{
								drugs_id: that.drugs_id,
								drugs: that.yaoDetail.dr_name,
								dr_model: that.yaoDetail.dr_model,
								number: that.number,
								explain: that.explain,
								Num2: that.Num2,
								riNum: that.riNum,
								pianNum: that.pianNum,
								neiNum: that.neiNum,
								remarks: that.remarks,
								dr_specifications:that.dr_specifications
							}]
			
							if (that.leixing == 1) {
								for (var i = 0; i < that.$store.state.yaoList.length; i++) {
									if (that.$store.state.yaoList[i].drugs_id == that.drugs_id) {
										uni.showToast({
											icon: "none",
											title: "处方里有当前药品,不能重复添加!"
										})
										that.added=1
										that.showadd = false
										return
									}
								}
								that.$store.state.yaoList = that.$store.state.yaoList.concat(datas)
								console.log(that.$store.state.yaoList)
							}
			// 编辑
							if (that.leixing == 2) {
								for (var i = 0; i < that.$store.state.yaoList.length; i++) {
									if (that.$store.state.yaoList[i].drugs_id == that.drugs_id) {
										that.$store.state.yaoList[i] = that.$store.state.yaoList[i].splice(i, 1, datas[0])
									}
								}
							}
			
							// const value = JSON.parse(uni.getStorageSync('yaoList'));
							// uni.setStorageSync('yaoList', JSON.stringify(datas.concat(value)));
			
							console.log(that.$store.state.yaoList, 88888)
							uni.navigateBack({
								delta: parseInt(that.cengNum)
							});
							// this.$nextTick(() => {
			
							// });
							
							that.yaoList=that.$store.state.yaoList
							console.log('that.yaoList2',that.yaoList)
							
							
							
							
							
						},
			
			tishi(){
				uni.showToast({
					title: '暂无库存',
					icon:'none',
					duration: 2000
				});
			},
			
			
			
			
		}
	}
</script>

<style scoped lang="scss">
.mainPage {
	display: flex;
	flex-direction: column;
}
.DrugsTop{
	width: 100%;
	background: #FFFFFF;
	z-index: 1;
	box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(79, 86, 98, 0.1);
	.input-box{
		width: 100%;
		padding:20rpx 32rpx;
		position: relative;
		image{
			position: absolute;
			top: 38rpx;
			left: 74rpx;
			display: block;
			width: 36rpx;
			height: 37rpx;
		}
		input{
			box-sizing: border-box;
			width: 100%;
			height: 72rpx;
			background: #F6F6F6;
			border-radius: 34rpx;
			border: 2rpx solid rgba(97, 123, 185, 0.07);
			padding: 0 140rpx 0  96rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #333;
			line-height: 72rpx;
		}
		.search{
			position: absolute;
			top: 20rpx;
			right: 32rpx;
			width: 140rpx;
			height: 72rpx;
			background: linear-gradient(215deg, #FF9331 0%, #FE7600 100%);
			border: 2rpx solid rgba(97, 123, 185, 0.07);
			text-align: center;
			border-radius: 34rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 68rpx;
		}
	}
}
.DrugsMain {
	flex: auto;
	position: relative;
	width: 100%;
	display: flex;
	.DrugsMainRight {
		position: absolute;
		width: 100%;
		height: 100%;
		right: 0;
		.drugslist {
			padding: 20rpx 24rpx 130rpx;
			:nth-last-child(1) {
				margin: 0;
			}
		}
		.list {
			margin-bottom: 54rpx;
			.titlelist {
				display: flex;
				align-items: center;
				.imgs{
					position: relative;
					background-color: #EEEEEE;
					border-radius: 10rpx;
					.types{
						position: absolute;right: 0; background-color: #F78361; width: 64rpx;height: 34rpx;
						font-size: 24rpx;
						color: #FFFFFF;text-align: center;border-radius: 12rpx 0;
					}
					image {
						width: 160rpx;
						height: 160rpx;
						border-radius: 10rpx;
					}
					
				}

				.title {
					padding: 0 24rpx;

					.name {

						font-size: 28rpx;
						color: #323232;
						padding: 4rpx 0;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.specifications {

						color: #909090;
						font-size: 24rpx;
						padding: 4rpx 0;
					}

					.stock {
						color: #909090;
						font-size: 24rpx;
						padding: 4rpx 0;
					}

					.pricelist {
						display: flex;
						// justify-content: space-between;
						align-items: center;

						.price {
							color: #F55050;
							font-weight: 600;
							font-size: 28rpx;
						}

						.Rebate {
							background: linear-gradient(180deg, #FFC364 0%, #FFE682 100%);
							padding: 2rpx 10rpx;
							font-size: 22rpx;
							color: #323232;
							border-radius: 12rpx;
							margin: 0 15rpx;
							
						}
					}
				}
			}
			.anniu {
				margin-top: 12rpx;
				background: linear-gradient(270deg, #FFC347 0%, #FF9331 100%);
				border-radius: 24rpx;
				font-size: 26rpx;
				width: 160rpx;
				color: #FFFFFF;
				padding: 6rpx 14rpx;
			}
		}
		.zanwuyuyue {
			text-align: center;
			image{
				display: block;
				margin: 300rpx auto 50rpx;
				width: 220rpx;
				height: 220rpx
			}
			text{
				font-size: 28rpx;
				font-weight: 400;
				color: #959595;
				line-height: 40rpx;
			}
		}
	}
}

.box1 {
	padding: 20rpx 32rpx;
	display: flex;
	// padding-top: 80rpx;
	.img {
		image {
			display: block;
			width: 132rpx;
			height: 132rpx;
		}
		margin-right: 12rpx;
	}
	.info {
		flex: auto;
		.name {
			font-size: 28rpx;
			font-weight: 500;
			color: #333333;
			line-height: 40rpx;
			margin-bottom: 14rpx;
		}
		.gongsi {
			font-size: 24rpx;
			font-weight: 400;
			color: #939499;
			line-height: 30rpx;
			margin-bottom: 14rpx;
		}
		.price {
			font-size: 28rpx;
			font-weight: 400;
			color: #DC1B31;
			line-height: 26rpx;
		}
	}
}
.box2 {
	margin-top: 15rpx;
	.shuliang-box {
		padding: 24rpx 32rpx;
		// border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
		display: flex;
		justify-content: space-between;
		align-items: center;
		.left {
			image {
				display: inline-block;
				width: 20rpx;
				height: 20rpx;
				vertical-align: middle;
				// margin-right: 20rpx;
			}
			text {
				font-size: 32rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
				vertical-align: middle;
				margin-left: 10rpx;
			}
		}
		.right {
			image {
				display: inline-block;
				width: 56rpx;
				height: 56rpx;
				vertical-align: middle;
			}
			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #343434;
				line-height: 40rpx;
				padding: 0 30rpx;
				vertical-align: middle;
			}
		}
	}
	.yongfa-box, .yongliang-box, .banfa-box {
		padding: 24rpx 0rpx 24rpx 32rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
		display: flex;
		justify-content: space-between;
		align-items: center;
		display: none;
		.left {
			image {
				display: inline-block;
				width: 20rpx;
				height: 20rpx;
				vertical-align: middle;
				// margin-right: 20rpx;
			}
			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
				vertical-align: middle;
			}
		}
		.right {
			input {
				width: 120rpx;
				height: 42rpx;
				// border-radius: 4rpx;
				// border: 2rpx solid rgba(0, 0, 0, 0.08);
				// padding-left: 18rpx;
				box-sizing: border-box;
				vertical-align: middle;
				display: inline-block;
				font-size: 24rpx;
				font-weight: 400;
				color: #333;
			}
			.wenben {
				display: inline-block;
				font-size: 28rpx;
				font-weight: 400;
				color: #242424;
				line-height: 40rpx;
				padding: 0 6rpx 0 12rpx;
				vertical-align: middle;
				.shijian{
					padding:6rpx 20rpx ;
					border: 2rpx solid #DCDCDC;
					border-radius: 12rpx;margin: 0 4rpx;
					font-size: 26rpx;
					color: #909090;
				}
				.active{
					color: #FFFFFF;
					background: -webkit-linear-gradient(#FFC347, #FF9331);
				}
			}
			image {
				display: inline-block;
				width: 14rpx;
				height: 20rpx;
				vertical-align: middle;
			}
		}
	}
	.beizhu-box {
		padding: 0 32rpx;
		.title {
			padding: 28rpx 0 20rpx 10rpx;
			font-size: 32rpx;
			font-weight: 400;
			color: #333333;
			line-height: 40rpx;
			display: inline-block;
		}
		textarea {
			width: 100%;
			box-sizing: border-box;
			height: 224rpx;
			// border-radius: 12rpx;
			// border: 2rpx solid #F6F6F6;
			font-size: 32rpx;
			font-weight: 400;
			padding: 24rpx 28rpx;
			line-height: 36rpx;
			color: #959595;
		}
	}
}
.btn-box {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	padding: 0 32rpx 100rpx;
	.btn {
		width: 100%;
		height: 98rpx;
		background: linear-gradient(219deg, #FF9331 0%, #FE7600 100%);
		border-radius: 8rpx;
		font-size: 36rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 98rpx;
		text-align: center;
	}
}

</style>
